(()=> {
	let ele = {};
	ele.modal_road = $('#modal-road');
	ele.close_road = $('#close-road');
	ele.list_furniture = $('#list-furniture');
	ele.list_prop = $('#list-prop');
	ele.list_talent = $('#list-talent');
	ele.list_totem = $('#list-totem');
	ele.modal_synthesis = $('#modal-synthesis');
	ele.close_synthesis = $('#close-synthesis');
	ele.yibox = $('#yibox');
	ele.modal_split = $('#modal-split');
	ele.close_split = $('#close-split');
	ele.buffList = $('#buffList');
	ele.modalbtn = $('#modalbtn');
	ele.nowhave = $('#nowhave');
	ele.queding = $('#queding');
	ele.chaiConfirm = $('#chaiConfirm');
	
	// 关闭 获取途径
	ele.close_road.onclick = () => {
		closeMdl(ele.modal_road);
	}
	//关闭 家具碎片合成
	ele.close_synthesis.onclick = () => {
		closeMdl(ele.modal_synthesis);
	}
	//关闭 拆分确认
	ele.close_split.onclick = () => {
		closeMdl(ele.modal_split);
	}
	realList();
	
	let bagList = '';
	
	// 背包列表
	function realList() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.REAL_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					drawList(res.data.menu_list);
					bagList = res.data.menu_list
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function drawList(_data) {
		let html1 = '';
		let html2 = '';
		let html3 = '';
		let html4 = '';
		// 家具
		// console.log(_data[2].list.length);
		if(_data[0].list.length == 0) {
			html1 = '<div class="empty"><img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/wu.png" alt=""><p>没有数据哦~</p></div>'
			ele.list_furniture.innerHTML = html1;
		} else {
			for(let i = 0; i < _data[0].list.length; i++) {
				// console.log(_data[0].list.length);
				let logo = IMG_DOMAIN+_data[0].list[i].logo
				let pj_type = _data[0].list[i].hammer == 6 ? 'itemred' :  _data[0].list[i].hammer == 2 ? 'itemgreen' : _data[0].list[i].hammer == 3 ? 'itemblue' :  _data[0].list[i].hammer == 4 ? 'itempurple' :  _data[0].list[i].hammer == 5 ? 'itemorange' : '';
				let sp_img = _data[0].list[i].jj_info.level == 0 ? '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">' : '';
				html1 += '<div class="item item1 '+pj_type+'">'
							+'<img class="itemimg1" src="'+logo+'" alt="">'
							+sp_img
							+'<span>'+_data[0].list[i].num+'</span>'
						+'</div>'
				ele.list_furniture.innerHTML = html1;
			}
		}
		
		// 道具
		if(_data[1].list.length == 0) {
			html2 = '<div class="empty"><img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/wu.png" alt=""><p>没有数据哦~</p></div>'
			ele.list_prop.innerHTML = html2;
		} else for(let i = 0; i < _data[1].list.length; i++) {
				let logo = IMG_DOMAIN+_data[1].list[i].logo
				let pj_type = _data[1].list[i].hammer == 6 ? 'itemred' :  _data[1].list[i].hammer == 2 ? 'itemgreen' : _data[1].list[i].hammer == 3 ? 'itemblue' :  _data[1].list[i].hammer == 4 ? 'itempurple' :  _data[1].list[i].hammer == 5 ? 'itemorange' : '';
				html2 += '<div class="item item2 '+pj_type+'">'
							+'<img class="itemimg1" src="'+logo+'" alt="">'
							+'<span>'+_data[1].list[i].num+'</span>'
						+'</div>'
			ele.list_prop.innerHTML = html2;
		}
		
		// 人才
		if(_data[2].list.length == 0) {
			html3 = '<div class="empty"><img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/wu.png" alt=""><p>没有数据哦~</p></div>'
			ele.list_talent.innerHTML = html3;
		} else for(let i = 0; i < _data[2].list.length; i++) {
			// else {
				let logo = IMG_DOMAIN+_data[2].list[i].logo
				let pj_type = _data[2].list[i].hammer == 6 ? 'itemred' :  _data[2].list[i].hammer == 2 ? 'itemgreen' : _data[2].list[i].hammer == 3 ? 'itemblue' :  _data[2].list[i].hammer == 4 ? 'itempurple' :  _data[2].list[i].hammer == 5 ? 'itemorange' : '';
				html3 += '<div class="item item3 '+pj_type+'">'
							+'<img class="itemimg1" src="'+logo+'" alt="">'
							+'<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">'
							+'<span>'+_data[2].list[i].num+'</span>'
						+'</div>'
			// }
			ele.list_talent.innerHTML = html3;
		}
		// 图腾
		if(_data[3].list.length == 0) {
			html4 = '<div class="empty"><img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/wu.png" alt=""><p>没有数据哦~</p></div>'
			ele.list_totem.innerHTML = html4;
		} else for(let i = 0; i < _data[3].list.length; i++) {
			 // else {
				let logo = IMG_DOMAIN+_data[3].list[i].logo
				let pj_type = _data[3].list[i].hammer == 6 ? 'itemred' :  _data[3].list[i].hammer == 2 ? 'itemgreen' : _data[3].list[i].hammer == 3 ? 'itemblue' :  _data[3].list[i].hammer == 4 ? 'itempurple' :  _data[3].list[i].hammer == 5 ? 'itemorange' : '';
				html4 += '<div class="item item4 '+pj_type+'">'
							+'<img class="itemimg1" src="'+logo+'" alt="">'
							+'<span>'+_data[3].list[i].num+'</span>'
						+'</div>'
			// }
			ele.list_totem.innerHTML = html4;
		}
		
		
		let tj_html = '';
		
		//  详情
		let item1s = $('.item1');
		for(let i = 0; i < item1s.length; i++) {
			item1s[i].onclick = ()=> {
				// bagList[0].list[i]
				let img = IMG_DOMAIN+bagList[0].list[i].logo
				let name = bagList[0].list[i].name
				let txt = bagList[0].list[i].describe
				let type = bagList[0].name
				let sp_img = _data[0].list[i].jj_info.level == 0 ? '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">' : '';
				let pj_type = _data[0].list[i].hammer == 6 ? 'itemred' :  _data[0].list[i].hammer == 2 ? 'itemgreen' : _data[0].list[i].hammer == 3 ? 'itemblue' :  _data[0].list[i].hammer == 4 ? 'itempurple' :  _data[0].list[i].hammer == 5 ? 'itemorange' : '';
				let is_show = _data[0].list[i].jj_info.re_id == 3 ? 'none' : 'block'
				let is_chai = _data[0].list[i].jj_info.level == 0 ? '合成' : '拆分'
				let is_type = _data[0].list[i].jj_info.level == 0 ? '0' : '1';
				tj_html = '<div class="buffTop">'
						  	+'<div class="thingimg  '+pj_type+'">'
						  		+'<img src="'+img+'" alt="">'
								+ sp_img
						  	+'</div>'
						  	+'<div class="thingroad">'
						  		+'<div class="titiler">'
						  			+'<p>'+name+'</p>'
						  			+'<span>类型：'+type+'</span>'
						  		+'</div>'
						  		+'<p class="remark">'+txt+'</p>'
						  	+'</div>'
						 +'</div>'
						 +'<div class="tujing">'
						  	+'<p>获得途径:</p>'
							+'<div class="ipt-box">'
								+'<span>幸运房东</span>'
								+'<a href="luck.html"><button>前往</button></a>'
						  	+'</div>'
						  	+'<div class="ipt-box">'
						  		+'<span>矿山搜索</span>'
						  		+'<a href="search.html"><button>前往</button></a>'
						  	+'</div>'
						  +'</div>'
						  console.log(_data[0].list[i].jj_info.re_id == 3 && _data[0].list[i].jj_info.level != 0);
				if(_data[0].list[i].jj_info.re_id == 3 && _data[0].list[i].jj_info.level != 0) {
					ele.modalbtn.innerHTML = '<button class="madbtn1" style="display: '+is_show+';" id="hecheng" data="'+bagList[0].list[i].id+'" data2="'+bagList[0].list[i].link_id+'">'+is_chai+'</button>'+'<a href="energy_Fang.html"><button class="madbtn2">使用</button></a>'
				}else {
					ele.modalbtn.innerHTML = '<button class="madbtn1" id="hecheng" data="'+bagList[0].list[i].id+'" data2="'+bagList[0].list[i].link_id+'">'+is_chai+'</button>'+'<a href="energy_Fang.html"><button class="madbtn2">使用</button></a>'
				}
				ele.buffList.innerHTML = tj_html;
				openMdl(ele.modal_road);
				let hecheng = $('#hecheng')
				hecheng.onclick = ()=> {
					let id = hecheng.getAttribute('data')
					let id2 = hecheng.getAttribute('data2')
					console.log(id);
					if(is_type == 0) {
						jjList(id)
					} else {
						userCfMsg(id,id2)
						// ele.queding.innerHTML = txt
						// openMdl(ele.modal_split);
						// ele.chaiConfirm.onclick = ()=> {
						// 	chaifenList(id)
						// }
					}
					// openMdl(ele.modal_synthesis)
				}
			}
		}
		
		//  详情  道具
		let item2s = $('.item2');
		for(let i = 0; i < item2s.length; i++) {
			item2s[i].onclick = ()=> {
				// bagList[0].list[i]
				let img = IMG_DOMAIN+bagList[1].list[i].logo
				let name = bagList[1].list[i].name
				let txt = bagList[1].list[i].describe
				let type = bagList[1].name
				// let sp_img = _data[1].list[i].jj_info.level == 0 ? '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">' : '';
				let pj_type = _data[1].list[i].hammer == 6 ? 'itemred' :  _data[1].list[i].hammer == 2 ? 'itemgreen' : _data[1].list[i].hammer == 3 ? 'itemblue' :  _data[1].list[i].hammer == 4 ? 'itempurple' :  _data[1].list[i].hammer == 5 ? 'itemorange' : '';
				// let is_chai = _data[1].list[i].jj_info == null ? '合成' : '拆分'
				let tujing = '';
				for(let j = 0; j < _data[1].list[i].get_list.length; j++) {
					let qian = _data[1].list[i].get_list[j].split(':')[0]
					let hou = _data[1].list[i].get_list[j].split(':')[1]
					// console.log(_data[1].list[i].get_list[j].split(':')[0]);
					tujing += '<div class="ipt-box">'
							  	+'<span>'+qian+'</span>'
							  	+'<a href="'+hou+'.html"><button>前往</button></a>'
							  +'</div>'
				}
				
				tj_html = '<div class="buffTop">'
						  	+'<div class="thingimg  '+pj_type+'">'
						  		+'<img src="'+img+'" alt="">'
								// + sp_img
						  	+'</div>'
						  	+'<div class="thingroad">'
						  		+'<div class="titiler">'
						  			+'<p>'+name+'</p>'
						  			+'<span>类型：'+type+'</span>'
						  		+'</div>'
						  		+'<p class="remark">'+txt+'</p>'
						  	+'</div>'
						 +'</div>'
						 +'<div class="tujing">'
						  	+'<p>获得途径:</p>'
							+ tujing
						  +'</div>'
						  
				ele.modalbtn.innerHTML = '<a href="'+ _data[1].list[i].use_link+'.html"><button class="madbtn2">使用</button></a>'
				// ele.modalbtn.innerHTML = ''
				ele.buffList.innerHTML = tj_html;
				openMdl(ele.modal_road);
			}
		}
		
		
		//  详情  人才
		let item3s = $('.item3');
		for(let i = 0; i < item3s.length; i++) {
			item3s[i].onclick = ()=> {
				// bagList[0].list[i]
				let img = IMG_DOMAIN+bagList[2].list[i].logo
				let name = bagList[2].list[i].name
				let txt = bagList[2].list[i].describe
				let type = bagList[2].name
				// let sp_img = _data[1].list[i].jj_info.level == 0 ? '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">' : '';
				let pj_type = _data[2].list[i].hammer == 6 ? 'itemred' :  _data[2].list[i].hammer == 2 ? 'itemgreen' : _data[2].list[i].hammer == 3 ? 'itemblue' :  _data[2].list[i].hammer == 4 ? 'itempurple' :  _data[2].list[i].hammer == 5 ? 'itemorange' : '';
				// let is_chai = _data[1].list[i].jj_info == null ? '合成' : '拆分'
				tj_html = '<div class="buffTop">'
						  	+'<div class="thingimg  '+pj_type+'">'
						  		+'<img src="'+img+'" alt="">'
								+ '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">'
						  	+'</div>'
						  	+'<div class="thingroad">'
						  		+'<div class="titiler">'
						  			+'<p>'+name+'</p>'
						  			+'<span>类型：'+type+'</span>'
						  		+'</div>'
						  		+'<p class="remark">'+txt+'</p>'
						  	+'</div>'
						 +'</div>'
						 +'<div class="tujing">'
						  	+'<p>获得途径:</p>'
						  	+'<div class="ipt-box">'
						  		+'<span>人才直聘，猎头直聘</span>'
						  		+'<a href="directEmploy.html"><button>前往</button></a>'
						  	+'</div>'
							+'<div class="ipt-box">'
								+'<span>砸蛋</span>'
								+'<a href="leyuan.html"><button>前往</button></a>'
							+'</div>'
						  +'</div>'
				ele.modalbtn.innerHTML = '<a href="abilityPerson.html"><button class="madbtn2">使用</button></a>'
				// ele.modalbtn.innerHTML = ''
				ele.buffList.innerHTML = tj_html;
				openMdl(ele.modal_road);
			}
		}
		
		
		//  详情 图腾
		let item4s = $('.item4');
		for(let i = 0; i < item4s.length; i++) {
			item4s[i].onclick = ()=> {
				// bagList[0].list[i]
				let img = IMG_DOMAIN+bagList[3].list[i].logo
				let name = bagList[3].list[i].name
				let txt = bagList[3].list[i].describe
				let type = bagList[3].name
				// let sp_img = _data[1].list[i].jj_info.level == 0 ? '<img class="itemimg2" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">' : '';
				let pj_type = _data[3].list[i].hammer == 6 ? 'itemred' :  _data[3].list[i].hammer == 2 ? 'itemgreen' : _data[3].list[i].hammer == 3 ? 'itemblue' :  _data[3].list[i].hammer == 4 ? 'itempurple' :  _data[3].list[i].hammer == 5 ? 'itemorange' : '';
				// let is_chai = _data[1].list[i].jj_info == null ? '合成' : '拆分'
				tj_html = '<div class="buffTop">'
						  	+'<div class="thingimg  '+pj_type+'">'
						  		+'<img src="'+img+'" alt="">'
								// + sp_img
						  	+'</div>'
						  	+'<div class="thingroad">'
						  		+'<div class="titiler">'
						  			+'<p>'+name+'</p>'
						  			+'<span>类型：'+type+'</span>'
						  		+'</div>'
						  		+'<p class="remark">'+txt+'</p>'
						  	+'</div>'
						 +'</div>'
						 +'<div class="tujing">'
						  	+'<p>获得途径:</p>'
							+'<div class="ipt-box">'
								+'<span>砸蛋</span>'
								+'<a href="leyuan.html"><button>前往</button></a>'
							+'</div>'
						  	+'<div class="ipt-box">'
						  		+'<span>原石切割</span>'
						  		+'<a href="mine.html"><button>前往</button></a>'
						  	+'</div>'
						  +'</div>'
						  
				ele.modalbtn.innerHTML = '<a href="home.html"><button class="madbtn2">使用</button></a>'
				// ele.modalbtn.innerHTML = ''
				ele.buffList.innerHTML = tj_html;
				openMdl(ele.modal_road);
			}
		}
	}
	
	
	// 合成家具列表
	function jjList(id) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.JJ_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: id // 当前家具碎片id
			},
			success: res => {
				if (res.code == 1) {
					drawlist_hc(res.data)
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 拆分家具 详情
	function userCfMsg(id,id2) {
		console.log(id2);
		let token = getCookie("token");
		ajax({
			url: API_PATH.USER_CF_MSG,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: id2
			},
			success: res => {
				if (res.code == 1) {
					ele.queding.innerHTML = res.data;
					openMdl(ele.modal_split);
					ele.chaiConfirm.onclick = ()=> {
						chaifenList(id)
					}
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 拆分 
	function chaifenList(id) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.FURNITURE_CHAI,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: id // 当前家具碎片id
			},
			success: res => {
				if (res.code == 1) {
					// drawlist_hc(res.data)
					openTips(res.msg);
					closeMdl(ele.modal_split);
					closeMdl(ele.modal_road);
					realList();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	
	function drawlist_hc(data) {
		let _data = data.list
		let html = '';
		for(let i = 0; i < _data.length; i++) {
			let img = IMG_DOMAIN + _data[i].jj.logo
			html += '<div class="yiji">'
						+'<div class="yileft">'
							+'<div class="leftin">'
								+'<img class="yijiimg" src="'+img+'" alt="">'
								+'<img class="suipian" src="http://img.qxgs.cc/tiantianlaishouzu/img/bag/suipian.png" alt="">'
							+'</div>'
							+'<div class="leftIn1">'
								+'<p>'+_data[i].jj.name+'</p>'
								+'<span>豪华值：+'+_data[i].luxury+'</span>'
							+'</div>'
						+'</div>'
						+'<div class="btnhc">'
							+'<button class="btnhcin" data="'+_data[i].id+'">'+data.has_num+'/'+_data[i].sj_num+'合成</button>'
						+'</div>'
					+'</div>'
		}
		ele.nowhave.innerHTML = '当前拥有碎片数量：' + data.has_num;
		ele.yibox.innerHTML = html;
		openMdl(ele.modal_synthesis);
		let btns = $('.btnhcin');
		for(let i = 0; i < btns.length; i++) {
			btns[i].onclick = ()=> {
				let id = btns[i].getAttribute('data')
				furnitureUpOne(id)
			}
		}
	}
	
	
	
	// 家具碎片合成
	function furnitureUpOne(id) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.FURNITURE_UP_ONE,
			method: 'POST',
			header: {
				'Authorization': token,
			},
			data: {
				id: id,// 当前家具碎片id
				is_bag: '1'
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					closeMdl(ele.modal_synthesis);
					realList();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
})()